package com.douhao.personal.service.impl;

import com.douhao.personal.mapper.RoleMapper;
import com.douhao.personal.model.Role;
import com.douhao.personal.service.RoleService;
import com.douhao.personal.util.Constants;
import com.douhao.personal.util.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
public class RoleServiceImpl implements RoleService {

    protected final static Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Resource
    RoleMapper roleMapper;

    @Override
    public Set<String> findRoleByUserId(String userId) {
        return roleMapper.findRoleByUserId(userId);
    }

    @Override
    public Page<Role> pageList(Role role, int pageNo, int pageSize) {
        Page<Role> page = new Page<>(pageNo, pageSize);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put(Constants.CODE_PAGE_START, page.getFirst());
        map.put(Constants.CODE_PAGE_SIZE, pageSize);
        map.put("role", role);//封装查询条件
        List<Role> roleList = roleMapper.list(map);
        long count = roleMapper.count(map);
        page.setResult(roleList);
        page.setTotalCount(count);
        return page;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return  roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> deleteRoleById(String ids) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        try {
            int count=0;
            String[] idArray = new String[]{};
            if(StringUtils.contains(ids, ",")){
                idArray = ids.split(",");
            }else{
                idArray = new String[]{ids};
            }

            for (String id : idArray) {
                count+=this.deleteByPrimaryKey(new Long(id));
            }
            resultMap.put("status", 200);
            resultMap.put("count", count);
        } catch (Exception e) {
            logger.debug("根据IDS删除角色出现错误，ids[%s]",getClass());
            resultMap.put("status", 500);
            resultMap.put("message", "删除出现错误，请刷新后再试！");
        }
        return resultMap;
    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        return 0;
    }

    @Override
    public int insert(Role role) {
        return roleMapper.insert(role);
    }
}
